module.exports = class extends think.Model {
  queryAll() {
    return think.cache('top_fee', () => {
      return this.field('days,fee').select();
    });
  }
  async queryFeeByDays(days) {
    const data = this.queryAll();
    for (let i = 0; i < data.length; i++) {
      if (days === data[i].days) {
        return data.fee;
      }
    }
    return null;
  }
  async create({
    days,
    fee
  }) {
    if (await this.where({days}).count() > 0) {
      think.throw('该置顶天数费用已存在');
    }
    return this.add({
      days,
      fee,
      create_time: think.datetime()
    });
  }
  // 修改
  async modifyById(id, {days, fee}) {
    if (await this.where({id: ['neq', id], days}).count() > 0) {
      think.throw('该置顶天数费用已存在');
    }
    return this.where({id}).update({
      days,
      fee,
      modify_time: think.datetime()
    });
  }
  // 删除
  physicDeleteById(id) {
    return this.where({id}).delete();
  }
};
